// components/bookDetail/submit/index.js
import { BookModel } from '../../../models/book.js';

const bookModel = new BookModel()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    bid: Number,
    comments: Array
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleHideSumit () {
      this.triggerEvent('on-cancel', {
        isShow: false
      }, {})
    },
    handleUpdateComments (content) {
      this.triggerEvent('on-update', {
        content
      }, {})
    },
    handleCancel (e) {
      this.handleHideSumit()
    },
    handleSubmit (e) {
      const { text, value } = e.detail
      let comment = text || value
      const length = comment.length

      if (length > 12) {
        wx.showToast({
          title: '评论最多12个字符',
          icon: 'none'
        })
      } else if (length > 0) {
        bookModel.submitComment(this.properties.bid, comment).then(res => {
          wx.showToast({
            title: `${comment} +1`,
            icon: 'none'
          })
          this.handleUpdateComments(comment)
          this.handleHideSumit()
        })
      } else {
        // 不处理
      }
    }
  }
})
